#!/bin/bash

###
#
#   Simple Ubuntu Command Line for Gmail
#   Nail Version 1.0
#
#
#   USAGE:
#       chmod 755 ./setup_gmail_command_line.sh
#       sudo ./setup_gmail_command_line.sh install
#
#   Update GMAIL_USER and GMAIL_PASS below for better automation.  Do not
#   include @gmail.com with user name.
#
###

#
# Editable Settings
#
GMAIL_USER='YOUR_GMAIL_USER_NAME'
GMAIL_PASS='YOUR_GMAIL_USER_PASSWORD'


#
# Non-Editable Settings
#
TODAY=`date +%Y%m%d`
SCRIPT=$(readlink -f $0)
SCRIPTDIR=`dirname $SCRIPT`
ARGS=("$@")
ARG_COUNT="$#"


#
# main functions
#
function parse_commandline() {
    echo "parsing command line: ${ARG_COUNT} parameters"

    if [ "${ARGS[0]}" == "install"  ]; then
        return
    else
        echo "to run: $SCRIPT install"
        exit 1
    fi
}

function preamble() {
    cat <<EOB

*** Ubuntu Gmail Command Line Script ***
*** http://klenwell.com/is/UbuntuCommandLineGmail ***

SETTINGS
script       : $SCRIPT
gmail user   : $GMAIL_USER
gmail pass   : $GMAIL_PASS

NOTES
You can edit the user and password settings by editing
this script.  If you do not do this, you will need to
edit the two config files at the end of the script.
Instructions will be provided when the script at the end.

You will be asked for your sudo password.  This is to
install the necessary packages.

EOB
    prompt_
}

function check_nail_in_repo() {
    PKG=nail
    sudo apt-cache showpkg $PKG | grep --silent "Package: $PKG"
    HAS_PKG_EQ_ZERO=$?

    if [[ "$HAS_PKG_EQ_ZERO" -ne "0" ]]; then
        cat <<EOB
nail was not found in your repositories.  To complete this script, you
will need to enable a repository with nail in it.  A simple way:

    $ echo -e "\n# Dapper Universe for nail\ndeb http://us.archive.ubuntu.com/ubuntu dapper main universe\n" | sudo tee -a /etc/apt/sources.list
    $ sudo apt-get update
    $ sudo apt-cache showpkg nail
EOB
        exit 1
    fi
}

function install_mail_packages() {
    sudo apt-get install msmtp nail
}

function install_certificate() {
    sudo apt-get install ca-certificates
    sudo update-ca-certificates
}

function configure_msmtp() {

    MSMTP_PATH=~/.msmtprc

    if [[ -e $MSMTP_PATH ]]; then
        echo "Your config file $MSMTP_PATH will be backed up to $MSMTP_PATH-$TODAY and overwritten"
        prompt_
        mv $MSMTP_PATH $MSMTP_PATH-$TODAY
    fi

    touch /tmp/msmtp.log
    cat <<EOF >$MSMTP_PATH
# config options: http://msmtp.sourceforge.net/doc/msmtp.html#A-user-configuration-file
defaults
logfile /tmp/msmtp.log

# gmail account
account gmail
auth on
host smtp.gmail.com
port 587
user $GMAIL_USER@gmail.com
password $GMAIL_PASS
from $GMAIL_USER@gmail.com
tls on
tls_trust_file /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt

# isp account (change caps below)
account isp
auth login
host SMTP.YOURISP.COM
port 25
user YOURNAME@ISP.COM
from YOURNAME@ISP.COM
password YOURPASSWORD

# set default account to use (from above)
account default : gmail
EOF

    chmod 600 $MSMTP_PATH
}

function configure_nail() {

    MAILRC_PATH=~/.mailrc

    if [[ -e $MAILRC_PATH ]]; then
        echo "Your config file $MAILRC_PATH will be backed up to $MAILRC_PATH-$TODAY and overwritten"
        prompt_
        mv $MAILRC_PATH $MAILRC_PATH-$TODAY
    fi

    cat <<EOF >$MAILRC_PATH
# set smtp for nail

# gmail account (default)
# $ nail -s "subject line" -a /path/file recipient@email.com < /path/body.txt
set from="$GMAIL_USER@gmail.com ($GMAIL_USER)"
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a gmail"

# isp account (add -A option to command line)
# $ nail -A isp -s "subject line" -a /path/file recipient@email.com < /path/body.txt
account isp {
set from="YOURNAME@ISP.COM"
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a isp"
}
EOF

}

function afterword() {

    cat <<EOB

-- The necessary packages and files have now been installed.

EOB

    if [ "$GMAIL_USER" == 'YOUR_GMAIL_USER_NAME' ]; then
        cat <<EOB

NOTE: You still need to complete the following steps:

    1. Set your email address and password in the
       file ~/.msmtprc (replace CAPS):

        $ gedit ~/.msmtprc

    2. Set your your user name in the file
       ~/.mailrc (replace CAPS):

        $ gedit ~/.mailrc

    3. Send a test email:

        $ echo -e "testing email from the command line" > /tmp/test_email
        $ nail -s "nail gmail test" YOURNAME@gmail.com < /tmp/test_email
        $ tail /tmp/msmtp.log

    4. Check your email to confirm that you received it.

EOB
    else
        # send test email
        echo "sending test email now"
        echo -e "testing email from the command line" > /tmp/test_email
        nail -s "nail gmail test" $GMAIL_USER@gmail.com < /tmp/test_email
        tail /tmp/msmtp.log

        cat <<EOB

A test email has been sent to $GMAIL_USER@gmail.com .  If successful,
you can login to your account now and find it.  You can also check
the mail log:

    $ tail /tmp/msmtp.log

EOB
    fi
}


#
# support functions
#
function prompt_() {
    read -s -n1 -p "Hit [ENTER] or [SPACE] to continue, any other key to quit " INPUT_
    if [ ! -e $INPUT_ ]; then
        echo -e "\nquitting now...\n"
        exit 1
    fi
    echo -e "\n"
}


#
# main
#
parse_commandline
preamble
check_nail_in_repo
install_mail_packages
install_certificate
configure_msmtp
configure_nail
afterword
